package com.cloudsigns.factorymanager2.project.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.project.bean.ProductBean;
import com.cloudsigns.factorymanager2.project.myinerface.ItemCheckedChangeListener;
import com.cloudsigns.factorymanager2.project.myinerface.ItemEditListener;
import com.cloudsigns.factorymanager2.project.myinerface.ItemRemoveListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 人员设置适配器
 * Created by tony on 2017/12/22.
 */

public class PersonnelSettingAdapter extends BaseAdapter {
    private Context context;
    private List<ProductBean> datas = new ArrayList<ProductBean>();
    private LayoutInflater inflater;
    private String tag = "";
    private List<String> selectedItemList = new ArrayList<String>();
    private ItemCheckedChangeListener itemCheckedChangeListener;

    public PersonnelSettingAdapter(Context context, String tag) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.tag = tag;
    }

    public void setData(List<ProductBean> list) {
        this.datas.clear();
        if (null != list) {
            this.datas.addAll(list);
        }

    }

    public void setItemCheckedChangeListener(ItemCheckedChangeListener itemCheckedChangeListener) {
        this.itemCheckedChangeListener = itemCheckedChangeListener;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public ProductBean getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();

            convertView = inflater.inflate(R.layout.item_personnel_setting_lv, null);
            holder.ll_item = convertView.findViewById(R.id.ll_item);
            holder.cb_item = convertView.findViewById(R.id.cb_item);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (position % 2 != 0) {
            holder.ll_item.setBackgroundColor(context.getResources().getColor(R.color.rgb_f2f2f2));
        } else {
            holder.ll_item.setBackgroundColor(context.getResources().getColor(R.color.rgb_ffffffff));
        }

        holder.cb_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemCheckedChangeListener != null) {
                    itemCheckedChangeListener.checkedChangeListener(position, holder.cb_item.isChecked());
                }
            }
        });
        return convertView;
    }

    private class ViewHolder {
        private LinearLayout ll_item;
        private CheckBox cb_item;
        private TextView tv_up;
        private TextView tv_down;
        private ImageView img_right;
        private ImageView img_left;
    }

}
